package cc.mrbird.febs.system.controller;

import cc.mrbird.febs.common.annotation.ControllerEndpoint;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.system.entity.SystemParam;
import cc.mrbird.febs.system.service.ISystemParamService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 系统参数表 Controller
 *
 * @author Mr.Yang
 * @date 2022-08-10 23:38:46
 */
@Slf4j
@Validated
@Controller
public class SystemParamController extends BaseController {

    @Autowired
    private ISystemParamService systemParamService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "systemParam")
    public String systemParamIndex(){
        return FebsUtil.view("systemParam/systemParam");
    }

    @GetMapping("systemParam")
    @ResponseBody
    @RequiresPermissions("systemParam:list")
    public FebsResponse getAllSystemParams(SystemParam systemParam) {
        return new FebsResponse().success().data(systemParamService.findSystemParams(systemParam));
    }

    @GetMapping("systemParam/list")
    @ResponseBody
    @RequiresPermissions("systemParam:list")
    public FebsResponse systemParamList(QueryRequest request, SystemParam systemParam) {
        Map<String, Object> dataTable = getDataTable(this.systemParamService.findSystemParams(request, systemParam));
        return new FebsResponse().success().data(dataTable);
    }

    @ControllerEndpoint(operation = "新增SystemParam", exceptionMessage = "新增SystemParam失败")
    @PostMapping("systemParam")
    @ResponseBody
    @RequiresPermissions("systemParam:add")
    public FebsResponse addSystemParam(@Valid SystemParam systemParam) {
        this.systemParamService.createSystemParam(systemParam);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "删除SystemParam", exceptionMessage = "删除SystemParam失败")
    @GetMapping("systemParam/delete")
    @ResponseBody
    @RequiresPermissions("systemParam:delete")
    public FebsResponse deleteSystemParam(SystemParam systemParam) {
        this.systemParamService.deleteSystemParam(systemParam);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改SystemParam", exceptionMessage = "修改SystemParam失败")
    @PostMapping("systemParam/update")
    @ResponseBody
    @RequiresPermissions("systemParam:update")
    public FebsResponse updateSystemParam(SystemParam systemParam) {
        this.systemParamService.updateSystemParam(systemParam);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改SystemParam", exceptionMessage = "导出Excel失败")
    @PostMapping("systemParam/excel")
    @ResponseBody
    @RequiresPermissions("systemParam:export")
    public void export(QueryRequest queryRequest, SystemParam systemParam, HttpServletResponse response) {
        List<SystemParam> systemParams = this.systemParamService.findSystemParams(queryRequest, systemParam).getRecords();
        ExcelKit.$Export(SystemParam.class, response).downXlsx(systemParams, false);
    }
}
